<template>
	<view class="page">
		<!-- 页面头部内容开始 -->
		<vk-data-page-header
			:title="page.title"
			:show-back="page.showBack"
			@back="pageBack"
		></vk-data-page-header>
		<!-- 页面头部内容结束 -->
		<view class="page-body">
			<!-- 页面主体内容开始 -->
			<view style="margin-bottom: 10px;font-size: 14px;">
				<view class="tips">注意：</view>
				<view class="tips">1、Lv.0 用户成为分销员后的初始等级</view>
				<view class="tips">2、赠送会员等级：当用户满足条件升级到此等级时，如果用户的会员等级低于此等级，则升级到此会员等级</view>
				<view class="tips">3、相同条件之间是 或 的关系，不同条件之间是 和 的关系</view>
				<view class="tips">4、简单模式：分销等级的升级条件只需要满足最低会员等级即可。可以只为某一个，或某几个分销等级开放简单升级模式。</view>
			</view>
			<vk-data-form
				ref="form1"
				v-loading="loading"
				v-model="form1.data"
				:rules="form1.props.rules"
				:action="form1.props.action"
				:form-type="form1.props.formType"
				:columns='form1.props.columns'
				:loading.sync="form1.props.loading"
				:labelWidth="form1.props.labelWidth"
				:footer-show="false"
				@success="formSuccess"
			></vk-data-form>
			<!-- 页面主体内容结束 -->
		</view>
		<view class="page-footer">
			<!-- 页面底部内容开始 -->
			<el-button plain style="margin-right: 20px;width: 80px;" @click="resetForm" :loading="form1.props.loading">重置</el-button>
			<el-button type="primary" style="width: 80px;" @click="submitForm" :loading="form1.props.loading">{{ page.submitText }}</el-button>
			<!-- 页面底部内容开始 -->
		</view>
		<view class="page-dialog">
			<!-- 页面弹窗内容开始 -->

			<!-- 页面弹窗内容开始 -->
		</view>
	</view>
</template>

<script>
	var that;													// 当前页面对象
	var vk = uni.vk;									// vk实例
	export default {
		data() {
			// 页面数据变量
			return {
				// init请求返回的数据
				mchInfo:{},
				data:{

				},
				loading: false,
				page: {
					title: "会员等级",
					submitText: "设置",
					cancelText: "返回",
					showBack: false,
					backPage:"",
				},
				form1: {
					// 表单请求数据，此处可以设置默认值
					data: {},
					// 表单属性
					props: {
						// 表单请求地址
						action: "admin/business/sys/mch.updateFxLevelRule",
						// 表单字段显示规则
						columns: [
							// 常用字段类型
							{
								key:"level_rule", title:"", type:"array<object>", showLabel:false,
								size:"small",
								showAdd:true,
								showClear:false,
								showSort:false,
								// 新增一行时,该行的默认值
								defaultValue:{
									name:"",
									discount:"",
									total_recharge_amount:"",
									total_order_amount:"",
									is_free_shipping:false,
									free_shipping_min_order_amount:"",
									integral_ratio:1,
									upgrade_mode:0,
									min_vip_lv:""
								},
								maxlength:10,
								rightBtns:[
									{
										mode:'delete',
										title:'删除',
										show: (item,index)=>{
											return index > 0;
										}
									}
								],
								columnIndexMethod:function(index){
									let str = `Lv.${index}`;
									return str;
								},
								// 每行每个字段对应的渲染规则
								columns:[
									{
										key:"name", title:"等级名称", type:"text", width:160, clearable:false, placeholder:"输入等级名称",
										isUnique:true,// 不可重复
										rules:[
											{ required:true, message:"该项不能为空", trigger:["change","blur"] },
											{ min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: ["change","blur"] }
										]
									},
									{ key:"share_reward_1", title:"直推奖励倍数", type:"number", width:130, placeholder:"倍数", rightText:"倍", incMode: 2, clearable:false },
									{ key:"share_reward_2", title:"间推奖励倍数", type:"number", width:130, placeholder:"倍数", rightText:"倍", incMode: 2, clearable:false },
									{ key:"min_vip_lv", title:"最低会员等级", placeholder:"会员等级", type:"select", width:140,
										data:()=>{
											return this.mchInfo.vip_level_rule || [];
										},
										props:{ value:'lv', label:'name', children:'children' },
										disabled:"upgrade_mode!=1",
										rules:[
											{ validator: (rule, value, callback, obj)=>{
												let that = this;
												let vk = this.vk;
												let str = Object.keys(obj)[0];
												let index1 = str.indexOf(".");
												let index2 = index1 + 1 + str.substring(index1+1).indexOf(".");
												if(index1 > -1 && index2 > -1){
													let index = Number(str.substring(index1+1,index2));
													if(!isNaN(index)){
														let item = that.form1.data.level_rule[index];
														if (item.upgrade_mode === 1 && vk.pubfn.isNull(value)) {
															return callback(new Error('请选择会员等级'));
														}
													}
												}
												callback();
											}, trigger: ['blur','change'] }
										]
									},
									{
										key:"upgrade_mode", title:"升级模式", type:"radio", width:200,
										optionType:"button",
										data:[
											{ value:1, label:"简单模式" },
											{ value:0, label:"常规模式" }
										],
										disabled:"lv==0"
									},
									{ key:"vip_lv", title:"赠送会员等级", type:"select", width:140, placeholder:"会员等级",
										data:()=>{
											return this.mchInfo.vip_level_rule || [];
										},
										props:{ value:'lv', label:'name', children:'children' },
										disabled:"upgrade_mode>0"
									},
									{ key:"total_recharge_amount", title:"条件1：累计充值", type:"money", width:140, placeholder:"金额",  rightText:"元", incMode: 2, clearable:false, disabled:"lv==0||upgrade_mode>0" },
									{ key:"total_order_amount", title:"条件1：累计消费", type:"money", width:140, placeholder:"金额",  rightText:"元", incMode: 2, clearable:false, disabled:"lv==0||upgrade_mode>0" },
									{ key:"total_per_1", title:"条件2：直推业绩", type:"money", width:150, placeholder:"金额",  rightText:"元", incMode: 2, clearable:false, disabled:"lv==0||upgrade_mode>0" },
									{ key:"total_share_1", title:"条件3：直推人数", type:"number", width:140, placeholder:"人数", rightText:"人", incMode: 2, clearable:false, disabled:"lv==0||upgrade_mode>0" },
								]
							}
						],
						// 表单验证规则
						rules: {

						},
						labelWidth: "100px"
					}
				}
			}
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady(){

		},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {

		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {

		},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options){
				vk.callFunction({
					url: 'client/pub/getMchInfo',
					loading: true,
					data: {

					},
					success: (data) => {
						this.mchInfo = data.mchInfo;
						this.$refs.form1.setFormData(data.mchInfo.fx);
					}
				});
			},
			// 页面跳转
			pageTo(path){
				vk.navigateTo(path);
			},
			// 页面返回
			pageBack(){
				const pages = getCurrentPages();
				if (pages.length>1 && pages[0].route !== that.$page.route) {
					vk.navigateBack();
				} else if(that.page.backPage) {
					vk.navigateTo(that.page.backPage);
				}
			},
			// 表单重置
			resetForm() {
				that.$refs.form1.resetForm();
			},
			// 表单提交
			submitForm(){
				that.$refs.form1.submitForm();
			},
			// 表单提交成功后的回调
			formSuccess(res){
				vk.setVuex("$user.mchInfo",res.data.info);
			}
		},
		// 监听器
		watch:{

		},
		// 计算属性
		computed:{

		}
	}
</script>
<style lang="scss" scoped>

</style>
